library(tidyverse)
library(ragg)
library(sf)

# this code was written before the release of sf
sf_use_s2(FALSE)

######
# ML Predictions
######
pred_ASM_robust_files <- list.files("Data/ML Prediction/Predictions",
                                    "prediction_model_ASM_\\d+_individual_hp\\.dill\\.csv")

pred_ASM_robust_seeds <- pred_ASM_robust_files %>% 
  str_split("_|\\.") %>% 
  sapply(`[`, 5) %>% 
  as.numeric()

voted_asm <- 
  lapply(paste0("Data/ML Prediction/Predictions/", pred_ASM_robust_files), read_csv) %>% 
  bind_rows()

#####
# Figure
#####

agg_png("./Figures/Figure-A-10-vote-share-across-seeds.png", width = 7, height = 4, units = "in", res = 300)
print(
voted_asm %>% 
  group_by(gid) %>% 
  summarise("ASM Share Agreement" = ifelse(sum(ASM) > (n()/2), sum(ASM) /n(), sum(!ASM)/n()), 
            "ASM Prediction" = sum(ASM) > (n()/2)) %>% 
  ungroup() %>% 
  ggplot() + 
  geom_histogram(aes(`ASM Share Agreement`, fill = `ASM Prediction` ), bins = 10) + 
  labs(title = "Agreement of ASM Prediction for Different Seeds")
)
dev.off()
